//
//  DDCreateOrderCellSectionFooter.m
//  Dingdong
//
//  Created by 程强 on 16/12/14.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDCreateOrderCellSectionFooter.h"

@interface DDCreateOrderCellSectionFooter()
@property (strong, nonatomic) UIView *giftView;
@property (strong, nonatomic) KKLabel *totalLbl;
@property (strong, nonatomic) UIImageView *giftIcon;
@property (strong, nonatomic) KKLabel *giftNameLbl;
@property (strong, nonatomic) KKLabel *freightLbl;
@property (strong, nonatomic) KKLabel *deliveryLbl;
@property (strong, nonatomic) UIView *deliveryView;
@property (strong, nonatomic) KKLabel *totalTitleLbl;
@end

@implementation DDCreateOrderCellSectionFooter
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBColor(244, 244, 244);
        
        self.giftView = [[UIView alloc]init];
        [self addSubview:_giftView];
        _giftView.backgroundColor = [UIColor whiteColor];
        [_giftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.equalTo(@50);
        }];
        
        KKLabel *giftLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"gift",RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [_giftView addSubview:giftLbl];
        [giftLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(_giftView);
            make.left.equalTo(_giftView).offset(15);
            make.width.equalTo(@30);
        }];
        
        self.giftIcon = [[UIImageView alloc]init];
        [_giftView addSubview:_giftIcon];
        _giftIcon.image = LoadImage(@"touxiang_test.jpg");
        [_giftIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_giftView);
            make.left.equalTo(giftLbl.mas_right).offset(10);
            make.width.height.equalTo(@40);
        }];
        
        self.giftNameLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",[UIColor blackColor],[UIFont systemFontOfSize:13.0],NSTextAlignmentLeft);
        }];
        _giftNameLbl.numberOfLines = 0;
        [_giftView addSubview:_giftNameLbl];
        [_giftNameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_giftView);
            make.left.equalTo(_giftIcon.mas_right).offset(10);
            make.right.equalTo(_giftView).offset(-15);
            make.height.equalTo(@40);
        }];
        
        self.deliveryView = [[UIView alloc]init];
        [self addSubview:_deliveryView];
        _deliveryView.backgroundColor = [UIColor whiteColor];
        [_deliveryView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(_giftView.mas_bottom).offset(1);
            make.height.equalTo(@50);
        }];
        
        KKLabel *deliveryTitleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"Delivery",RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [_deliveryView addSubview:deliveryTitleLbl];
        [deliveryTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_deliveryView);
            make.left.equalTo(_deliveryView).offset(15);
            make.width.equalTo(@60);
            make.height.equalTo(@30);
        }];
        
        self.deliveryLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentRight);
        }];
        [_deliveryView addSubview:_deliveryLbl];
        [_deliveryLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_deliveryView);
            make.right.equalTo(_deliveryView).offset(-15);
            make.left.equalTo(deliveryTitleLbl.mas_right).offset(15);
            make.height.equalTo(@30);
        }];
        
        UIView *topBgView = [[UIView alloc]init];
        [self addSubview:topBgView];
        topBgView.backgroundColor = [UIColor whiteColor];
        [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(_deliveryView.mas_bottom).offset(1);
            make.height.equalTo(@50);
        }];
        
        KKLabel *freightTitleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"Discount",RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [topBgView addSubview:freightTitleLbl];
        [freightTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(topBgView);
            make.left.equalTo(topBgView).offset(15);
            make.width.equalTo(@80);
        }];
        
        self.freightLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",RGBColor(255, 80, 80),[UIFont systemFontOfSize:15.0],NSTextAlignmentRight);
        }];
        [topBgView addSubview:_freightLbl];
        [_freightLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(topBgView);
            make.right.equalTo(topBgView).offset(-20);
            make.left.equalTo(freightTitleLbl.mas_right).offset(10);
        }];
        
//
//        UIView *line = [[UIView alloc]init];
//        line.backgroundColor = RGBColor(237, 237, 237);
//        [self addSubview:line];
//        [line mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.equalTo(self);
//            make.top.equalTo(topBgView.mas_bottom);
//            make.height.equalTo(@1);
//        }];
        
        UIView *bottomBgView = [[UIView alloc]init];
        [self addSubview:bottomBgView];
        bottomBgView.backgroundColor = [UIColor whiteColor];
        [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(topBgView.mas_bottom).offset(1);
            make.height.equalTo(@50);
        }];
        
        KKLabel *couponTitleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"Coupon",RGBColor(51, 51, 51),[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [bottomBgView addSubview:couponTitleLbl];
        [couponTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(bottomBgView);
            make.left.equalTo(bottomBgView).offset(15);
            make.width.equalTo(@80);
        }];
        
        UIImageView *nextIcon = [[UIImageView alloc]init];
        nextIcon.image = LoadImage(@"next_arrow");
        [bottomBgView addSubview:nextIcon];
        [nextIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bottomBgView);
            make.right.equalTo(bottomBgView).offset(-15);
            make.width.equalTo(@8);
            make.height.equalTo(@13);
        }];
        
        self.couponLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"Coupon",RGBColor(255, 80, 80),[UIFont systemFontOfSize:15.0],NSTextAlignmentRight);
        }];
        [bottomBgView addSubview:_couponLbl];
        [_couponLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.height.equalTo(bottomBgView);
            make.right.equalTo(nextIcon).offset(-15);
            make.left.equalTo(couponTitleLbl.mas_right).offset(10);
        }];

        UIButton *couponBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [bottomBgView addSubview:couponBtn];
        [couponBtn addTarget:self action:@selector(couponClick:) forControlEvents:UIControlEventTouchUpInside];
        [couponBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(bottomBgView);
        }];
        
        UIView *priceView = [[UIView alloc]init];
        [self addSubview:priceView];
        priceView.backgroundColor = [UIColor whiteColor];
        [priceView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(bottomBgView.mas_bottom).offset(1);
            make.height.equalTo(@50);
        }];
        
        self.totalLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"",RGBColor(255, 80, 80),[UIFont systemFontOfSize:15.0],NSTextAlignmentLeft);
        }];
        [priceView addSubview:_totalLbl];
        [_totalLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(priceView);
            make.right.equalTo(priceView).offset(-15);
            make.width.equalTo(@80);
            make.height.equalTo(@30);
        }];
        
        self.totalTitleLbl = [[KKLabel alloc]initWithBlock:^(KKLabel *lbl) {
            lbl.laText(@"共1件商品，小计：",[UIColor blackColor],[UIFont systemFontOfSize:15.0],NSTextAlignmentRight);
        }];
        [priceView addSubview:_totalTitleLbl];
        [_totalTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(priceView);
            make.right.equalTo(_totalLbl.mas_left);
            make.height.equalTo(@30);
            make.left.equalTo(priceView).offset(20);
        }];
    }
    return self;
}
- (void)couponClick:(UIButton *)sender
{
    if (self.validCouponBlock) {
        self.validCouponBlock();
    }
}
- (void)userChoseValidCouponClick:(ChoseValidCoupon)click
{
    self.validCouponBlock = click;
}
- (void)setTotalMoney:(NSString *)totalMoney
{
    _totalMoney = totalMoney;
    _totalLbl.text = totalMoney;
    CGFloat width = [ToolKit widthWithFont:[UIFont systemFontOfSize:15.0] height:30 text:totalMoney];
    [_totalLbl mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(width+10));
    }];
}
- (void)setShippingPrice:(NSString *)shippingPrice
{
    _shippingPrice = shippingPrice;
    self.freightLbl.text = shippingPrice;
}
- (void)setDelivery:(NSString *)delivery
{
    _delivery = delivery;
    _deliveryLbl.text = delivery;
}
- (void)setDiscount:(NSString *)discount
{
    _discount = discount;
    _freightLbl.text = FormatString(@"$%@", discount);
}
- (void)setGoodsNum:(NSInteger)goodsNum
{
    _goodsNum = goodsNum;
    _totalTitleLbl.text = FormatString(@"Total of %ld products:", goodsNum);
}
- (void)setHasGift:(BOOL)hasGift
{
    _hasGift = hasGift;
    if (!hasGift) {
        [_giftView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@0);
        }];
        [_deliveryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_giftView.mas_bottom);
        }];
        _giftView.hidden = YES;
    }else{
        [_giftView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@50);
        }];
        [_deliveryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_giftView.mas_bottom).offset(1);
        }];
        _giftView.hidden = YES;
    }
}
- (void)setGiftName:(NSString *)giftName
{
    _giftName = giftName;
    _giftNameLbl.text = giftName;
}
- (void)setGiftIconUrl:(NSString *)giftIconUrl
{
    _giftIconUrl = giftIconUrl;
    [_giftIcon sd_setImageWithURL:[NSURL URLWithString:giftIconUrl] placeholderImage:nil];
}
@end
